package kafka

import (
	"context"
	"fmt"
	"my-zero-tiktok/apps/api/internal/svc"
	"my-zero-tiktok/apps/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type KafkaPushLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewKafkaPushLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KafkaPushLogic {
	return &KafkaPushLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *KafkaPushLogic) KafkaPush(req *types.KafkaPush) (resp *types.Empty, err error) {
	go func() {
		for i := 0; i < 3; i++ {
			msg := fmt.Sprintf("%s ==> %d", req.Message, i)
			if err := l.svcCtx.KqProducer.Push(l.ctx, msg); err != nil {
				l.Logger.Errorf("KqPusherClient Push Error , err :%v", err)
			}
		}
	}()
	return &types.Empty{}, nil
}
